﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Entity;
using NLBS.Framework;

namespace NLBS.Data.Business
{
    public class PageCacheHelp
    {
        public static readonly string OneArticleKey = "OneArticleKey:ID{0}";
        public static readonly string ArticleCatPageKey = "ArticleCatPage:Index{0}PageSize{1}CatID{2}";
        public static readonly String ArticleArrayKey = "ArticleCatArray{0}";
        public IPagedCollection<T> GetPageFromCatCatche<T>()
        {
            PageSetting settings = Setting;
            ContentCache cache = ContentCache.Instantiate();
            IList<int> listKey = (List<int>)cache[string.Format(ArticleArrayKey, CatID)];
            IPagedCollection<T> lists = new PagedCollection<T>();
            if (listKey != null)
            {
                lists.MaxItems = listKey.Count;
                if (settings.End > listKey.Count)
                {
                    settings.End = listKey.Count;
                }

                for (int i = settings.Start; i < settings.End; i++)
                {
                  
                    T art = (T)cache[string.Format(settings.Replicekey, listKey[i])];
                    if (art != null)
                    {
                        lists.Add(art);
                    }
                    else
                    {
                       
                        lists.Clear();
                        return lists;
                    }
                }
               
            }
            return lists;
        }


        public PageCacheHelp(int catID, IPagedCollection<Article> lists, PageSetting settings)
        {
            this.CatID = catID;
            this.List = lists;
            this.Setting = settings;
        }

        public void SetCache()
        {
            //初始化数组长度
            InitListLength<Article>();
            //为日志分类添加ID列表
            AddArticleIDCache();
            //添加日志缓存
            AddArticleCacheKey();
        }
        #region  property
        private int _catID;

        public int CatID
        {
            get { return _catID; }
            set { _catID = value; }
        }

        private IPagedCollection<Article> _list;

        public IPagedCollection<Article> List
        {
            get { return _list; }
            set { _list = value; }
        }

        private PageSetting _setting;

        public PageSetting Setting
        {
            get { return _setting; }
            set { _setting = value; }
        }

        #endregion



        public void InitListLength<T>()
        {
            ContentCache cache = ContentCache.Instantiate();
            IList<int> catListKey = (List<int>)cache[string.Format(ArticleArrayKey, CatID)];
            if (catListKey == null)
            {
                catListKey = new List<int>();

                for (int i = 0; i < List.MaxItems; i++)
                {
                    catListKey.Add(0);
                }
            }
            cache.Insert(string.Format(ArticleArrayKey, CatID), catListKey, CacheDuration.Long);
        }

        public void AddArticleIDCache()
        {
            
            ContentCache cache = ContentCache.Instantiate();
            IList<int> catListKey = (List<int>)cache[string.Format(ArticleArrayKey, CatID)];
            if (Setting.End > catListKey.Count)
            {
                Setting.End = catListKey.Count;
            }
            for (int i = Setting.Start; i < Setting.End; i++)
            {
                int art = List[i - Setting.Start].ArticleID;
                if (art > 0)
                {
                    catListKey[i] = art;
                }
            }
        }

        public void AddArticleCacheKey()
        {
            ContentCache cache = ContentCache.Instantiate();
            foreach (Article item in List)
            {
                cache.Insert(string.Format(OneArticleKey, item.ArticleID), item, CacheDuration.Long);
            }
        }
    }
}
